import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:path_provider/path_provider.dart';
import 'package:universal_io/io.dart';

///封装各种方法

///收起键盘
unfocus(BuildContext context) {
  FocusScope.of(context).unfocus();
}

///收起键盘
requestFocus(BuildContext context, {FocusNode? focusNode}) {
  if (focusNode != null) {
    FocusScope.of(context).requestFocus(focusNode);
  } else {
    FocusScope.of(context).requestFocus();
  }
}

///延时
delay(int ms, VoidCallback voidCallback) {
  Future.delayed(Duration(milliseconds: ms), voidCallback);
}

///等待
Future<void> wait(int ms) async {
  await Future.delayed(Duration(milliseconds: ms));
}

///获取应用程序目录
Future<Directory> getApplicationDocumentsDir() =>
    getApplicationDocumentsDirectory();

/////////GetX方法/////////

///返回
void back<T>({
  T? result,
  bool closeOverlays = false,
  bool canPop = true,
  int? id,
}) {
  Get.back(result: result, closeOverlays: closeOverlays, canPop: canPop);
}

///进入页面
void goto(
  dynamic page, {
  dynamic arguments,
  Transition? transition,
}) {
  Get.to(page, arguments: arguments, transition: transition);
}

///进入页面
Future<T?>? gotoNamed<T>(
  String pageName, {
  dynamic arguments,
}) {
  return Get.toNamed(pageName, arguments: arguments);
}

///进入页面并关闭之前所有页面
void gotoNamedAndOffAll(
  String pageName, {
  dynamic arguments,
}) {
  Get.offAllNamed(pageName, arguments: arguments);
}

///进入页面并关闭之前所有页面
Future<T?> openDialog<T>(
  Widget widget, {
  Object? arguments,
  Color? barrierColor,
  Duration? transitionDuration,
}) {
  return Get.dialog<T>(widget,
      arguments: arguments,
      barrierColor: barrierColor ?? Colors.black12,
      transitionDuration: transitionDuration);
}

Future<T?> openBottomSheet<T>(Widget widget) {
  return Get.bottomSheet(widget);
}

///强制更新APP所有小部件
Future<void> forceAppUpdate() {
  return Get.forceAppUpdate();
}
